package com.guoguo.controller;

import com.alibaba.fastjson.JSONObject;
import com.guoguo.base.BaseController;
import com.guoguo.base.PageBean;
import com.guoguo.base.PaginationContext;
import com.guoguo.entity.AcCategoryAlbum;
import com.guoguo.entity.Album;

import com.guoguo.entity.vo.*;
import com.guoguo.enums.AlbumEnum;
import com.guoguo.enums.AliOssImageEnum;
import com.guoguo.service.*;
import com.guoguo.utils.AliOssUtils;
import com.guoguo.utils.FileUtils;
import com.guoguo.utils.StringUtils;
import com.guoguo.utils.UUIDUtils;
import com.guoguo.utils.aliOss.AliOssImageConfig;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/23.
 */
@Controller
@RequestMapping("/view")
public class AlbumController extends BaseController {
    private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ModelController.class);
    @Resource
    private AlbumService albumService;
    @Resource
    private ICategoryService iCategoryService;

    @Resource
    private ModelsService modelsService;

    @Resource
    private IImagesService iImagesService;

    @Autowired

    private UploadService uploadService;

    @Autowired
    private IAcCategoryAlbumService categoryAlbumService;


    /**
     * 查询所有专辑
     *
     * @return
     */
    @RequestMapping(value = "/statement", method = RequestMethod.GET)
    public ModelAndView album(@RequestParam int pageNumber, @RequestParam int pageSize) {
        Map<String, Object> data = new HashedMap();
        PageBean<AlbumsVO> statement1 = albumService.selectAlbumList(pageNumber, pageSize);

        data.put("albumList", statement1);
        return returnView("/album/albumList", data);
    }

    /**
     * 去跳转添加/修改专辑
     *
     * @return
     */
    @RequestMapping(value = "/albumAdd", method = RequestMethod.GET)
    public ModelAndView Album(String albId) {
        Map<String, Object> data = new HashedMap();
        List<CategoryVO> categories = iCategoryService.getCategoryList();
        List<ModelsVO> modelsVOs = modelsService.selectModelList();
        List<CategoryAlbumVO> categoryAlbumVOs = categoryAlbumService.selectCategoryAlbum();
        //分类列表
        data.put("cateList", categories);
        //模特列表
        data.put("modelList", modelsVOs);

//        data.put("categoryAlbumList", categoryAlbumVOs);


        if ("".equals(albId) || albId == null) {
            data.put("albId", UUIDUtils.getUUID());
        } else {
            AlbumsVO album = albumService.selectByPrimaryKey(albId);
            //专辑所属分类
            List<AcCategoryAlbumVO> acCategoryAlba = categoryAlbumService.getCategoryAlbumList(albId);
            //专辑下所有图片
            PageBean<AlbumImagesVO> albumImageList = iImagesService.getAlbumImageList(albId);
            //判断专辑所属分类
            for (CategoryVO category : categories) {
                for (AcCategoryAlbumVO acCategoryAlbumVO : acCategoryAlba) {
                    if (category.getCateId().equals(acCategoryAlbumVO.getCateId())) {
                        category.setIsShow(10);
                    }
                }
            }

            StringBuffer freeImage = new StringBuffer();
            for (AlbumImagesVO albumImagesVO : albumImageList.getList()) {
                if (albumImagesVO.getIsFree() == 0) {
                    freeImage.append(albumImagesVO.getImgId()).append(",");
                }
            }

            data.put("imagesList", albumImageList.getList());
            data.put("albId", album.getAlbId());
            data.put("albName", album.getAlbName());
            data.put("originalPrice",album.getOriginalPrice());
            data.put("presentPrice",album.getPresentPrice());
            data.put("modelId", album.getModelId());
            data.put("freeImage", freeImage);
            data.put("albDescription", album.getAlbDescription());
            data.put("albConver", acCategoryAlba);
        }
        return returnView("/album/albumAdd", data);
    }

    //添加/修改专辑
    @ResponseBody
    @RequestMapping(value = "/saveOrUpdateAlbum", method = RequestMethod.POST)
    public JSONObject saveAlbum(@RequestParam String albId, @RequestParam String albName, @RequestParam String albDescription,@RequestParam String originalPrice,@RequestParam String presentPrice,
                                @RequestParam String modelId, @RequestParam String cateIds, @RequestParam String imgIds, @RequestParam String converIds) {
        if ("".equals(cateIds)) {
            return returnResult(AlbumEnum.ALBUM_ARGS_ERROR.getId(), "", AlbumEnum.ALBUM_ARGS_ERROR.getValue());
        }
        if ("".equals(imgIds)) {
            return returnResult(AlbumEnum.ALBUM_ARGS_ERROR.getId(), "", AlbumEnum.ALBUM_ARGS_ERROR.getValue());
        }
        //作者id
        boolean flag = albumService.addAlbum(albId, albName, albDescription, originalPrice,presentPrice,"F36B0F1C47EB491AA924BE2D960BB750", modelId, cateIds, imgIds, converIds);
        return returnResult(200, flag, "");
    }

    /**
     * 根据albId删除专辑及关联数据
     *
     * @param albId
     * @return
     */
    @RequestMapping(value = "/deleteAlbum", method = RequestMethod.GET)
    public String deleteAlbum(String albId) {
        int i = albumService.deleteAlbumInfo(albId);
        return "redirect:/view/statement?pageNumber=1&pageSize=10";
    }


    /**
     * 封面上传

     * @param fileName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uploadCover", method = RequestMethod.POST)
    public JSONObject uploadFile(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile fileName, @RequestParam String cateId) {
        List<String> list = FileUtils.springFileUpload(request, 0);
        List<String> ossFile = uploadService.uploadFile(list);
        JSONObject jsonObject = new JSONObject();
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        if (ossFile.size() > 0) {
            jsonObject.put("coverUrl", AliOssUtils.getImageUrl(ossFile.get(0)));//, aliOssImageConfig));
            cateId = StringUtils.replace(cateId, "\"", "");
            logger.debug("=======>>>   cateId:" + cateId);
            AcCategoryAlbum acCategoryAlbum = categoryAlbumService.findCategoryAlbumById(cateId);
            logger.debug("=======>>>   ossFile path:" + ossFile.get(0));
            logger.debug("==========>>>>>>>    acCategoryAlbum is:" + acCategoryAlbum.getCateId());
            acCategoryAlbum.setCoverUrl(ossFile.get(0));
            categoryAlbumService.updateActegoryAlbum(acCategoryAlbum);
        }
        return returnResult(200, jsonObject, "");
    }


}








